﻿using HelenServer.Migrator;

namespace HelenServer.BugEngine.Dal;

internal sealed class MigrateCustomFieldHandler
{
    private MigrateCustomFieldHandler()
    {

    }

    public static IEnumerable<TEntity> Map<TEntity>()
        where TEntity : class
    {
        var type = typeof(TEntity);

        return type == typeof(BugType)
            ? (IEnumerable<TEntity>)MapBugTypes()
            : type == typeof(Solution)
            ? (IEnumerable<TEntity>)MapSolutions()
            : type == typeof(Os)
            ? (IEnumerable<TEntity>)MapOss()
            : type == typeof(Browser)
            ? (IEnumerable<TEntity>)MapBrowsers() : throw new NotImplementedException();
    }

    private static IEnumerable<BugType> MapBugTypes()
    {
        var ztTypes = MigrateProvider.DbCache.ZtLangs.Where(n => n.Section == "typeList").ToList();
        ztTypes.RemoveAll(n => string.IsNullOrEmpty(n.Value));

        return ztTypes.Select(n => new BugType { Name = n.Value });
    }

    private static IEnumerable<Solution> MapSolutions()
    {
        var ztSolutions = MigrateProvider.DbCache.ZtLangs.Where(n => n.Section == "resolutionList").ToList();
        ztSolutions.RemoveAll(n => string.IsNullOrEmpty(n.Value));

        return ztSolutions.Select(n => new Solution { Name = n.Value });
    }

    private static IEnumerable<Os> MapOss()
    {
        var ztOss = MigrateProvider.DbCache.ZtLangs.Where(n => n.Section == "osList").ToList();
        ztOss.RemoveAll(n => string.IsNullOrEmpty(n.Value));
        var osEntities = ztOss.Select(n => new Os { Name = n.Value });

        return ztOss.Select(n => new Os { Name = n.Value });
    }

    private static IEnumerable<Browser> MapBrowsers()
    {
        var ztBrowsers = MigrateProvider.DbCache.ZtLangs.Where(n => n.Section == "browserList").ToList();
        ztBrowsers.RemoveAll(n => string.IsNullOrEmpty(n.Value));
        return ztBrowsers.Select(n => new Browser { Name = n.Value });

    }
}